// pages/user/account/cardList/cardList.js

const app = getApp();
let api = require('../../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardList: [],
    delBtnWidth: 180 //删除按钮宽度单位（rpx）
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

    var that = this;
    var token = wx.getStorageSync('TOKEN');
    that.getBandCardList(token);
    var list = that.data.cardList
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  //  onShareAppMessage: function() {

//  },

  //手指刚放到屏幕触发
  touchS: function(e) {
   // console.log("touchS" + e);
    //判断是否只有一个触摸点
    if (e.touches.length == 1) {
      this.setData({
        //记录触摸起始位置的X坐标
        startX: e.touches[0].clientX
      });
    }
  },
  //触摸时触发，手指在屏幕上每移动一次，触发一次
  touchM: function(e) {
   // console.log(e);
    var that = this
    if (e.touches.length == 1) {
      //记录触摸点位置的X坐标
      var moveX = e.touches[0].clientX;
      //计算手指起始点的X坐标与当前触摸点的X坐标的差值
      var disX = that.data.startX - moveX;
      //delBtnWidth 为右侧按钮区域的宽度
      var delBtnWidth = that.data.delBtnWidth;
      var txtStyle = "";
      if (disX == 0 || disX < 0) { //如果移动距离小于等于0，文本层位置不变
        txtStyle = "left:0px";
      } else if (disX > 0) { //移动距离大于0，文本层left值等于手指移动距离
        txtStyle = "left:-" + disX + "rpx";
        if (disX >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度
          txtStyle = "left:-" + delBtnWidth + "rpx";
        }
      }
      //获取手指触摸的是哪一个item
      var index = e.currentTarget.dataset.index;
      var list = that.data.cardList;
      //将拼接好的样式设置到当前item中
      list[index].txtStyle = txtStyle;
      list[index].isMove = true;
      //更新列表的状态
      this.setData({
        cardList: list
      });
    }
  },
  touchE: function(e) {
   // console.log(e.currentTarget.dataset);
    var that = this
    if (e.changedTouches.length == 1) {
      //手指移动结束后触摸点位置的X坐标
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = that.data.startX - endX;
      var delBtnWidth = that.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0px";
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      var list = that.data.cardList;
     // console.log(index);
      list[index].txtStyle = txtStyle;
      //更新列表的状态
      that.setData({
        cardList: list
      });
    }
  },
  /**
   * 新增银行卡
   */
  addCard: function() {
    wx.navigateTo({
      url: '../addCard/addCard',
    })
  },
  /**
   * 获取银行卡列表
   */
  getBandCardList: function(token) {
    var that = this;
    api._get('/wallet/bank/list', '', token).then(res => {
      if (res.code == 200) {
        var list = res.result;
        if (list.length == 0) {
          wx.removeStorage({
            key: 'CARDID',
            success: function(res) {},
          });
          wx.removeStorage({
            key: 'CARDNO',
            success: function(res) {},
          });
          wx.removeStorage({
            key: 'BANKNAME',
            success: function(res) {},
          });

        } else {

          for (var i = 0; i < list.length; i++) {
            var string = '';
            var str = list[i]['cardNo'];
            for (let j = 0; j < str.length - 8; j++) {
              string += '*';
            }
            var number = str.substr(0, 4) + string + str.substr(str.length - 4, str.length)
            list[i].txtStyle = "";
            list[i].isMove = false;
            list[i].number = number
          }

          wx.setStorageSync('BANKNAME', list[0].bankName);
          wx.setStorageSync('CARDNO', list[0].cardNo);
          wx.setStorageSync('CARDID', list[0].id);

        }

        that.setData({
          cardList: list
        })
      } else if (res.code > 400) {
        wx.navigateTo({
          url: '../login/login',
        })
      } else {
        wx.showToast({
          title: '网络忙请重试',
          icon: 'none',
        })
      }

    });
  },
  /**
   * 删除银行卡
   */
  delAddress: function(e) {
    var that = this;
    var token = wx.getStorageSync('TOKEN');
    var id = e.currentTarget.dataset.addressid;
    var param = {
      id: id
    };
    api._delete('/wallet/bank/del/' + id, param, token).then(res => {
      if (res.code == 200) {
        that.getBandCardList(token);
      } else {
        wx.showToast({
          title: '网络忙请重试',
          icon: 'none'
        })
      }
    })
  },
  /**
   * 选择银行卡
   */
  choose:function(e){
   // console.log(e.currentTarget.dataset.index)
    var that=this;
    var index=e.currentTarget.dataset.index;
    var data=that.data.cardList[index];
   // console.log(data);

    wx.setStorageSync('BANKNAME', data.bankName);
    wx.setStorageSync('CARDNO', data.cardNo);
    wx.setStorageSync('CARDID', data.id);
    wx.navigateBack({
      
    })

  }
})